using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using Entities;
using Bll;

public partial class Service_RegisterControl : System.Web.UI.UserControl
{
    
    protected void Page_Load(object sender, EventArgs e)
    {
        lbltaxcode.Visible = false;
        txtTaxcode.Visible = false;
        RequiredFieldValidatorTaxcode.IsValid = true;
        RegularExpressionValidatorTaxcode.IsValid = true;
       
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        CustomerBll custBll = new CustomerBll();
        AccountBll accBll = new AccountBll();

        CustomerInfo cust = new CustomerInfo();
        AccountInfo acc = new AccountInfo();

        if (!accBll.CheckExistAccount(txtUserName.Text))
        {
            lblCheckUsername.Text = "";
            cust.FirstName = txtFirstName.Text;
            cust.LastName = txtLastName.Text;
            cust.Address = txtAddress.Text;
            cust.City = txtCity.Text;
            cust.Phone = txtPhone.Text;
            cust.Email = txtEmail.Text;
            DateTime dt = new DateTime(int.Parse(txtyear.Text), int.Parse(ddlmonth.SelectedValue), int.Parse(ddlday.SelectedValue));
            cust.DateOfBirth = dt;
            cust.BankName = txtBankName.Text;
            cust.AccountNumber = txtAccountNumber.Text;
            cust.TaxCode = txtTaxcode.Text;         
            cust.UserName = txtUserName.Text;

            acc.Username = txtUserName.Text;
            acc.Password = txtPassword.Text;
            acc.SecretQuestion = ddlSecretQuestion.SelectedValue;
            acc.SecretAnswer = txtSecretAnswer.Text;
            if (string.IsNullOrEmpty(txtTaxcode.Text))
                acc.Role = "customer";
            else
                acc.Role = "dealer";
            accBll.Insert(acc);
            custBll.Insert(cust);
            Response.Redirect("dangnhap.aspx");
        }
        else
        {
            lblCheckUsername.Text = "Validation Username";
        }
       

       
    }
    
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        resetFields();
    }
    protected void btnCheckUsername_Click(object sender, EventArgs e)
    {

        AccountBll accBll = new AccountBll();
        
        if (!string.IsNullOrEmpty(txtUserName.Text))
        {
            RegularExpressionValidatorUserName.Validate();
            if (RegularExpressionValidatorUserName.IsValid)
            {
                if (accBll.CheckExistAccount(txtUserName.Text))
                {
                    lblCheckUsername.Text = "This nickname already exist ! type another !";
                }
                else
                {
                    lblCheckUsername.Text = "Ok ! Your nick name is good to go !";
                }
            }
            else
            {
                lblCheckUsername.Text = "";
            }
        }
        else
        {
            lblCheckUsername.Text = "Input Username";
        }
       
        
    }
    protected void lbtnDealer_Click(object sender, EventArgs e)
    {
        lbltaxcode.Visible = true;
        txtTaxcode.Visible = true;
       
    }
    public void resetFields()
    {
        txtFirstName.Text = "";
        txtLastName.Text = "";
        txtAddress.Text = "";
        txtCity.Text = "";
        txtAccountNumber.Text = "";
        txtEmail.Text = "";
        txtUserName.Text = "";
        txtBankName.Text = "";
        txtPassword.Text = "";
        txtPhone.Text = "";
        txtRetypepassword.Text = "";
        txtSecretAnswer.Text = "";
        txtyear.Text = "";
        lblCheckUsername.Text = "";
        txtTaxcode.Text = "";
    }
}
